package com.example.graduation.controller.jianLi;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.graduation.entity.pojo.Delivery;
import com.example.graduation.service.DeliveryService;
import com.example.graduation.util.ShiroUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author Administrator
 */
@RequestMapping("/jianli")
@Controller
@Slf4j(topic = "JianLiController")
@Api(tags = "带有jianLi路径的controller")
public class JianLiController {
    private static final Integer PAGE_SIZE = 10;

    @Resource
    private DeliveryService deliveryService;

    /**
     * 投递简历功能
     *
     * @param imageId 简历ID
     * @return 投递结果
     */
    @PostMapping("/delivery/{imageId}/{companyId}")
    @ResponseBody
    @ApiOperation("投递简历")
    public boolean delivery(@PathVariable("imageId") String imageId, @PathVariable("companyId") String companyId) {
        QueryWrapper<Delivery> wrapper = new QueryWrapper<>();
        wrapper.eq("delivery_user_id", ShiroUtil.getUserId());
        wrapper.eq("delivery_accept_id", companyId);
        Delivery delivery = deliveryService.getOne(wrapper);
        if (delivery != null) {
            log.info("简历已经投递！");
            return false;
        } else {
            delivery = new Delivery();
            delivery.setDeliveryId(UUID.randomUUID().toString());
            delivery.setDeliveryUserId(ShiroUtil.getUserId());
            delivery.setDeliveryAcceptId(companyId);
            deliveryService.save(delivery);
            log.info("投递简历成功！");
        }
        return true;
    }

    /**
     * 查看当前用户已经投递的简历
     *
     * @return PageInfo
     */
    @PostMapping("/isDelivery/{pageNumber}")
    @ResponseBody
    @ApiOperation("查看当前用户已经投递的简历")
    public PageInfo<Delivery> isDelivery(@PathVariable("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Delivery> wrapper = new QueryWrapper<>();

        wrapper.eq("delivery_user_id", ShiroUtil.getUserId());
        List<Delivery> list = deliveryService.list(wrapper);
        return new PageInfo<>(list);
    }
}
